﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UmlDesigner.Model;

namespace UmlDesigner.Gui
{
    public partial class NewMethodForm : Form
    {
        internal NewMethodForm(UmlMethod m)
        {
            InitializeComponent();
            Icon = Properties.Resources.ApplicationIcon;

            method = m;
            workingMethod = new UmlMethod();
            method.DeepCopy(workingMethod);

            // Initialize all the controls to the values of the specified method.
            nameTextBox.Text = m.name;
            accessComboBox.Text = m.accessLevel;
            returnTypeTextBox.Text = m.returnType;
            virtualComboBox.Text = m.virtualModifier;

            foreach (Argument arg in m.arguments)
            {
                argsDataGridView.Rows.Add(arg.name, arg.type);
            }
        }

        private void OnAddNewArgClicked(object sender, EventArgs e)
        {
            Argument arg = new Argument();
            NewArgumentForm newArgForm = new NewArgumentForm(arg);
            if (newArgForm.ShowDialog() == DialogResult.OK)
            {
                workingMethod.arguments.Add(arg);
                argsDataGridView.Rows.Add(arg.name, arg.type);
            }
        }

        private void OnOkClicked(object sender, EventArgs e)
        {
            workingMethod.name = nameTextBox.Text;
            workingMethod.returnType = returnTypeTextBox.Text;
            workingMethod.accessLevel = accessComboBox.Text;
            workingMethod.virtualModifier = virtualComboBox.Text;

            workingMethod.DeepCopy(method);
        }

        private void OnRemoveArgClicked(object sender, EventArgs e)
        {
            if (argsDataGridView.SelectedRows.Count == 0)
                return;

            foreach (Argument arg in workingMethod.arguments)
            {
                DataGridViewRow selectedRow = argsDataGridView.SelectedRows[0];

                if (arg.name == (string)selectedRow.Cells[0].Value)
                {
                    workingMethod.arguments.Remove(arg);
                    argsDataGridView.Rows.Remove(selectedRow);
                    return;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            TypeSelectionForm dlg = new TypeSelectionForm();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                returnTypeTextBox.Text = dlg.selectedType;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MethodImplementationForm dlg = new MethodImplementationForm(workingMethod);
            dlg.ShowDialog();
        }

        UmlMethod method;
        UmlMethod workingMethod;

    }
}
